<?php

	header('Content-Type: text/html; charset=UTF-8');


	include './include/conf.php';
	
	if ($_POST['traitement']=="supprimer_flux_visibles")
	{
		$titreFlux = $_POST['titreFlux'];
		$selectSiteFlux =$pdo->prepare('DELETE FROM fluxrssvisibles WHERE titre_Flux="'.$titreFlux.'";');
		$selectSiteFlux->execute();
		echo "ok";
	}
	
	if ($_POST['traitement']=="ajouter_flux_visibles")
	{
		$site = $_POST['site'];
		$adresse = $_POST['adresse'];
		$titreFlux = $_POST['titreFlux'];
		$description = $_POST['description'];
		
		$selectSiteFlux =$pdo->prepare('select * from fluxrssvisibles where titre_flux="'.$titreFlux.'"');
		$selectSiteFlux->execute();
		
		if ($selectSiteFlux->rowCount()!= 0)
		{
			echo "nok:estVisible";
		}
		else
		{
			$selectSiteFlux =$pdo->prepare('INSERT INTO fluxrssvisibles(site, titre_flux, adresse_flux, description) VALUES ("'.$site.'","'.$titreFlux.'","'.$adresse.'","'.$description.'");');
			$selectSiteFlux->execute();
			echo "ok";
		}
	}

	

	
	if ($_POST['traitement']=="ajout")
	{
		$site = $_POST['site'];
		$adresse = $_POST['adresse'];
		
		$siteExiste = false;
		$adresseExiste = false;
		
		$selectSiteFlux =$pdo->prepare('select * from sitefluxrss where site="'.$site.'"');
		$selectSiteFlux->execute();
		
		if ($selectSiteFlux->rowCount()!= 0)
		{
			$siteExiste = true;
		}
		
		$selectSiteFlux =$pdo->prepare('select * from sitefluxrss where adresse="'.$adresse.'"');
		$selectSiteFlux->execute();
		
		if ($selectSiteFlux->rowCount()!= 0)
		{
			$adresseExiste = true;
		}
		
		if (($site == "Nom du site ...") || ($adresse == "Adresse ...") || ($site == "") || ($adresse == ""))
		{
			echo "nok:incorrect";
		}
		else
		{
			if (($siteExiste || $adresseExiste))
			{
				echo "nok:existe";
			}
			else 
			{
				$selectSiteFlux =$pdo->prepare('INSERT INTO sitefluxrss(site,adresse) VALUES ("'.$site.'","'.$adresse.'");');
				$selectSiteFlux->execute();
				echo "ok";
			}
		}
	}
		
	if ($_POST['traitement']=="supprimer")
	{
		$site = $_POST['site'];
		$selectSiteFlux =$pdo->prepare('DELETE FROM sitefluxrss WHERE site="'.$site.'";');
		$selectSiteFlux->execute();
		echo "ok";
	}
	
	/**************************************************************************/
	
	/**************************************************************************/
	
	if ($_POST['traitement']=="flux_visibles")
	{
	
		$tabReponse = array();
		$donnees = array();
		
		$selectSiteFlux =$pdo->prepare('select * from fluxrssvisibles');
		$selectSiteFlux->execute();
		
		while($siteFlux =  $selectSiteFlux->fetch())
		{
			$donnees[] = $siteFlux;
		}
		$selectSiteFlux->closeCursor();
		$tabReponse['fluxrssvisibles'] = $donnees;
		
		echo json_encode($tabReponse);
	}
	
	/**************************************************************************/
	
	/**************************************************************************/
	
	if ($_POST['traitement']=="liste")
	{
		$tabReponse = array();
		$donnees = array();
		
		$selectSiteFlux =$pdo->prepare('select * from sitefluxrss');
		$selectSiteFlux->execute();
		while($siteFlux =  $selectSiteFlux->fetch()){
			
			$donnees[] = $siteFlux;
		}
		$selectSiteFlux->closeCursor();
		$tabReponse['listesites'] = $donnees;
		
		echo json_encode($tabReponse);
	}
	
	/**************************************************************************/
	
	/**************************************************************************/
	
	if ($_POST['traitement']=="charger")
	{
		$tabReponse = array();
		$donnees = array();
		
		$selectSiteFlux =$pdo->prepare('select site, adresse from sitefluxrss');
		$selectSiteFlux->execute();
		while($siteFlux =  $selectSiteFlux->fetch())
		{
			$donnees[] = $siteFlux;
		}
		
		$selectSiteFlux->closeCursor();
		$tabReponse['siteflux'] = $donnees;
		
		echo json_encode($tabReponse);
	}
?>